package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishPageVo;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional//开启事务管理
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    /*
     * 新增菜品
     * */
    public void save(DishDTO dishDTO) {
        //1.创建实体类,来接收前端传来的数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dish.setStatus(StatusConstant.ENABLE);//状态
        //添加菜品后获取菜品id
        dishMapper.insert(dish);//不用补充基本属性可以自动填充,添加菜品后id属性会付给参数

        //添加口味

        List<DishFlavor> flavors = dishDTO.getFlavors();//获取口味对象
        if (flavors == null || flavors.size() == 0) {
            return;
        }
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
        }
        dishFlavorMapper.insertBatch(flavors);

    }

    /*
     * 菜品分页查询
     * */
    public Result page(DishPageQueryDTO dishPageQueryDTO) {
        //1.执行分页
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        //2.执行查询

        Page<DishVO> page = (Page<DishVO>) dishMapper.pageQuery(dishPageQueryDTO);

        //3.封装结果
        return Result.success(new DishPageVo(page.getTotal(), page.getResult()));


    }


    /*
     * 批量删除菜品
     * */
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                //起售中的菜品不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //跟套餐关联的菜品也不能删除
        List<Long> setmealIdsByDishIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIdsByDishIds != null && setmealIdsByDishIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除菜品对应的口味数据
            dishFlavorMapper.deleteByDishId(id);
        }


    }

    /*
     * 修改商品起售禁售信息
     * */

    public void startOrStop(Integer status, Long id) {

        //1.根据id获取dish
        Dish dish = dishMapper.getById(id);
        //2.设置起售禁售状态
        dish.setStatus(status);

        //3.执行修改
        dishMapper.update(dish);//不用补充基本属性,可以自动填充


    }

    /*
    * 根据菜品id来查询菜品即口味
    * */
    public Result<DishVO> getById(Long id) {
        DishVO dishVO = dishMapper.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /*
    * 修改菜品
    * */
    public void update(DishDTO dishDTO) {
        //修改菜品基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //删除菜品的口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //添加新的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();//口味集合可能前端不传菜品id所以要遍历赋值
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
